<?php
declare(strict_types=1);
namespace app\common\widget;
use aphp\core\Widget;
/**
 * 根据书籍ID或文档ID获取文档排序数据  ID-标题
 */
class DocSort extends Widget
{
    protected string $tag = 'doc';
    protected int $expire = 0;

    // $id 书籍ID $options['doc_id'] 文档ID
    public function set($id = '', array $options = []): array
    {
        $data = \widget('common.book_dir')->get($id, $options);
        return $this->_collect($data);
    }

    // 递归生成
    protected function _collect(array $tree, array &$result = []): array
    {
        foreach ($tree as $vo) {
            $result[$vo['id']] = $vo['title'];
            if (!empty($vo['children'])) {
                $this->_collect($vo['children'], $result);
            }
        }
        return $result;
    }
}